package com.fh.shop.controller;

import com.fh.common.baseData.CommonData;
import com.fh.common.baseData.TongyiData;
import com.fh.shop.entity.po.CarData;
import com.fh.shop.service.CarInfoService;
import com.fh.user.entity.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/* 购物车 相关的api  */
@RestController
@RequestMapping("authcarinfo")
public class CarInfoController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private HttpServletRequest request;
    @Autowired
    private CarInfoService carInfoService;

    @PostMapping("add")
    public TongyiData addCarInfo(Integer id, Integer sid, Integer count) {
        carInfoService.addCarInfo(id, sid, count);
        return TongyiData.succeess("");
    }

    @PostMapping("queryData")
    public TongyiData queyrData() {
        User u = (User) request.getAttribute("u");
        List<CarData> carDataList = (List<CarData>) redisTemplate.opsForHash().get(CommonData.REDIS_KEY + CommonData.REDIS_PRO, u.getName());
        return TongyiData.succeess(carDataList);
    }


    @PostMapping("upCarInfo")
    public TongyiData upCarInfo(String ids) {
        carInfoService.upCarInfo(ids);
        return TongyiData.succeess("");
    }
}
